﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PMS.XCMG.Util;

namespace PMS.XCMG.Entity
{
    /// <summary>
    /// 分拣料位存入物料规格设置
    /// </summary>
    public partial class MaterielSortingLocation : BusEntity
    {
        /// <summary>
        /// 分拣配置编号
        /// </summary>
        public string SortingConfigId { set; get; }

        /// <summary>
        /// 缓存位编号
        /// </summary>
        public string LocationId { set; get; }

        /// <summary>
        /// 待抓取物料规格编码
        /// </summary>
        public string SpecId { set; get; }

        /// <summary>
        /// 最大容量
        /// </summary>
        public int Quantity { set; get; }

        /// <summary>
        /// 数据采集地址(料框叶片型号,值为叶片型号中文名称)
        /// </summary>
        public string CollectorAddress { set; get; }
    }

    public partial class MaterielSortingLocation : BusEntity
    {
        /// <summary>
        /// 分拣规则
        /// </summary>
        public MaterielSortingConfig SortingConfig { set; get; }

        /// <summary>
        /// 料位信息
        /// </summary>
        public StorageLocation Location { set; get; }

        /// <summary>
        /// 待抓取物料规格
        /// </summary>
        public MaterielSpec Spec { set; get; }

        /// <summary>
        /// 参与抓取顺序
        /// </summary>
        public List<MaterielSortingSequence> Sequences { set; get; }
    }

    /// <summary>
    /// 实体配置
    /// OnModelCreating
    /// </summary>
    public class MaterielSortingLocationTypeConfig : BusEntityTypeConfig<MaterielSortingLocation>, IEntityTypeConfiguration<MaterielSortingLocation>
    {
        public override void Configure(EntityTypeBuilder<MaterielSortingLocation> builder)
        {
            base.Configure(builder);

            #region 主外键关系
            builder.HasOne(p => p.SortingConfig).WithMany(p => p.Locations).HasForeignKey(p => p.SortingConfigId);
            #endregion

            #region 字段属性:最大长度,是否必需,默认值
            builder.Property(p => p.SortingConfigId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.LocationId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.SpecId).HasMaxLength(EntityDefinition.DefSmallColLen);
            builder.Property(p => p.Quantity).IsRequired();
            builder.Property(p => p.CollectorAddress);
            #endregion

            #region 备注
            builder.HasComment("分拣料位存入物料规格设置");
            builder.Property(p => p.SortingConfigId).HasComment("分拣配置编号");
            builder.Property(p => p.LocationId).HasComment("缓存位编号");
            builder.Property(p => p.SpecId).HasComment("待抓取物料规格编码");
            builder.Property(p => p.Quantity).HasComment("最大容量");
            builder.Property(p => p.CollectorAddress).HasComment("数据采集地址");
            #endregion

            #region 种子数据
            var quantity = 34;
            for(var idy=1;idy<11;idy++)
            {
                for (var idx = 1; idx < 16; idx++)
                {
                    if(idy<10)
                    {
                        builder.HasData(new MaterielSortingLocation()
                        {
                            Id = Guid.NewGuid().ToString("n"),
                            SortingConfigId = string.Format("MSC_0{0}", idy),
                            LocationId = idx.ToString(),
                            SpecId = null,
                            CollectorAddress=string.Format("M{0}B{1}_VaneSpecName", idy.ToString().PadLeft(2,'0'),idx.ToString().PadLeft(2,'0')),
                            Quantity = quantity,
                            CreateUserId = "1",
                            ModifyUserId = "1"
                        });
                    }
                    else
                    {
                        builder.HasData(new MaterielSortingLocation()
                        {
                            Id = Guid.NewGuid().ToString("n"),
                            SortingConfigId = string.Format("MSC_{0}", idy),
                            LocationId = idx.ToString(),
                            SpecId = null,
                            CollectorAddress = string.Format("M{0}B{1}_VaneSpecName", idy.ToString().PadLeft(2, '0'), idx.ToString().PadLeft(2, '0')),
                            Quantity = quantity,
                            CreateUserId = "1",
                            ModifyUserId = "1"
                        });
                    }
                }
            }
            #endregion
        }
    }
}
